package com.shark.commons.context;

import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.shark.commons.constants.BaseConst;
import com.shark.commons.spring.SpringContextHolder;
import com.shark.commons.utils.IpUtil;
import com.shark.manager.system.model.User;
import com.shark.shiro.ShiroAuthRealm;

public class SystemHelper {
	
	/**
	 * 得到session
	 */
	public static Session getSession() {
		Session session = null;
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			session = subject.getSession(true);
		}
		return session;
	}

	/**
	 * 保存信息到session
	 */
	public static void setSessionAttibute(final String key, final Object value) {
		Session session = getSession();
		if (session != null) {
			session.setAttribute(key, value);
		}
	}

	/**
	 * 从session获取属性
	 */
	public static Object getSessionAttibute(final String key) {
		Object resutlt = null;
		Session session = getSession();
		if (session != null) {
			resutlt = session.getAttribute(key);
		}
		return resutlt;
	}

	/**
	 * 获取当前session的ip
	 */
	public static String getCurrentUserIp() {
		return getSession().getHost();
	}
	
	/**
	 * 获取当前HttpSession的ip
	 */
	public static String getCurrentUserHttpIp() {
		return IpUtil.getIpAddress(SpringContextHolder.getHttpRequest());
	}
	
	/**
	 * 退出系统并清空session
	 */
	public static void logout() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			subject.logout();
		}
	}

	/**
	 * 保存当前用户
	 */
	public static void setCurrentUser(final User user) {
		HttpSession session = SpringContextHolder.getHttpSession();
		if (session != null) {
			//保存用户信息到session
			session.setAttribute(BaseConst.CURRENT_USER, user);
		}
	}
	
	/**
	 * 得到当前用户
	 */
	public static User getCurrentUser() {
		User user = null;
		HttpSession session = SpringContextHolder.getHttpSession();
		if (null != session) {
			user = (User) session.getAttribute(BaseConst.CURRENT_USER);
		}
		return user;
	}
	
	/**
	 * 得到当前用户id
	 */
	public static Long getCurrentUserId() {
		return getCurrentUser().getId();
	}
	
	/**
	 * 得到当前用户用户名
	 */
	public static String getCurrentUsername() {
		return getCurrentUser().getAccount();
	}

	/**
	 * 得到当前用户真实姓名
	 */
	public static String getCurrentTruename() {
		return getCurrentUser().getTruename();
	}
	
	/**
	 * 获取验证码错误次数
	 * @return
	 */
	public static Integer getSessionCaptchaRetryCount() {
		Integer count = (Integer) SystemHelper.getSession().getAttribute(BaseConst.SESSION_CAPTCHA_ERROR_COUNT) ;
		if(count == null) {
			count = 1 ;
		}
		return count ;
	}
	
	/**
	 * 设置验证码错误次数
	 * @param count
	 * @return
	 */
	public static void setSessionCaptchaRetryCount(Integer count) {
		getSession().setAttribute(BaseConst.SESSION_CAPTCHA_ERROR_COUNT, count) ;
	}
	
	/**
	 * 登录成功后清除验证码错误次数
	 * @param count
	 * @return
	 */
	public static void removeSessionCaptchaRetryCount() {
		getSession().removeAttribute(BaseConst.SESSION_CAPTCHA_ERROR_COUNT) ;
	}
	
	/**
	 * 获取用户验证登录的错误次数
	 * @param count
	 * @return
	 */
	public static AtomicInteger getSessionRetryCount() {
		return (AtomicInteger) SystemHelper.getSession().getAttribute(BaseConst.SESSION_RETRY_COUNT);
	}
	
	/**
	 * 清除用户验证登录错误次数
	 * @param count
	 * @return
	 */
	public static void removeSessionRetryCount() {
		SystemHelper.getSession().removeAttribute(BaseConst.SESSION_RETRY_COUNT);
	}
	
	
	/**
	 * 清除缓存认证
	 */
	public static void clearCachedAuthenticationInfo() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
			ShiroAuthRealm userRealm = (ShiroAuthRealm) securityManager.getRealms().iterator().next();
			System.out.println(subject.getPrincipals());
			userRealm.clearCachedAuthenticationInfo(subject.getPrincipals());
		}
	}

	/**
	 * 清除缓存授权
	 */
	public static void clearCachedAuthorizationInfo() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
			ShiroAuthRealm userRealm = (ShiroAuthRealm) securityManager.getRealms().iterator().next();
			userRealm.clearCachedAuthorizationInfo(subject.getPrincipals());
		}
	}
}
